package demo.propertySupport;

/**
 * description: <br>
 * createTime: 2021/1/13 17:41 <br>
 *
 * @author zzh
 */
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;


public class SomeBean
{
    protected PropertyChangeSupport propertySupport; //属性改变通知支持
    protected VetoableChangeSupport vetoableChangeSupport; //反对属性改变支持
    private String changableValue;


    public SomeBean()
    {
        propertySupport = new PropertyChangeSupport(this);//使本对象有属性改变通知监听器的能力
        vetoableChangeSupport = new VetoableChangeSupport(this);////使监听器有反对本对象属性改变的能力
        //这里添加支持与象使用observer模式的实现接有口异曲同工之效
    }


    public void setChangableValue(String newValue) throws PropertyVetoException
    {
        String oldValue;
        if((null == changableValue) || !changableValue.equals(newValue))
        {
            oldValue = changableValue;
            changableValue = newValue;
            vetoableChangeSupport.fireVetoableChange("changableValue", oldValue, newValue);//这个在前
            propertySupport.firePropertyChange("changableValue", oldValue, newValue);//这个在后
            //如果两个监听器的次序调过来，你想想会有什么问题？你期待的效果没有达到哦~~~~~~~~~~~~~~~
        }
    }


    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
        propertySupport.addPropertyChangeListener(listener);
    }


    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
        propertySupport.removePropertyChangeListener(listener);
    }


    public void addVetoableChangeListener(VetoableChangeListener listener)
    {
        vetoableChangeSupport.addVetoableChangeListener(listener);
    }


    public void removeVetoableChangeListener(VetoableChangeListener listener)
    {
        vetoableChangeSupport.removeVetoableChangeListener(listener);

    }


    public String getChangableValue()
    {
        return changableValue;
    }

}